package com.gl.order.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gl.order.commom.ResultBean;
import com.gl.order.entity.OrderSize;
import com.gl.order.entity.TUser;
import com.gl.order.entity.dto.CustomerPageDto;
import com.gl.order.entity.vo.UserAndRoleCodeVo;
import com.gl.order.security.model.SecurityUser;
import com.gl.order.service.CustomerService;
import com.gl.order.service.OrderSizeService;
import com.gl.order.service.TMenuService;
import com.gl.order.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author: liangSY
 * @Date: 2021/6/26
 * @ClassName: CommonDataController
 * @Description: CommonDataController描述
 */
@Api(tags = "通用")
@RestController
@RequestMapping("commonData")
public class CommonDataController {

    private UserService userService;

    private TMenuService menuService;

    private OrderSizeService orderSizeService;

    private CustomerService customerService;

    public CommonDataController(UserService userService,TMenuService menuService,OrderSizeService orderSizeService, CustomerService customerService) {
        this.userService = userService;
        this.menuService = menuService;
        this.orderSizeService = orderSizeService;
        this.customerService = customerService;
    }

    @GetMapping("/size")
    @ApiOperation("尺码列表")
    public ResultBean<List<OrderSize>> sieList(@RequestParam("sizeType") String sizeType){
        LambdaQueryWrapper<OrderSize> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(OrderSize::getSizeType, sizeType);
        List<OrderSize> result = orderSizeService.list(queryWrapper);
        return ResultBean.success(result);
    }

    @GetMapping("/user")
    @ApiOperation("根据用户获取菜单")
    public ResultBean<List<Tree<String>>> three(){
        SecurityUser principal = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        TUser user = principal.getCurrentUserInfo();
        List menus = menuService.threeByUser(user.getId());
        return ResultBean.success(menus);
    }

    @ApiOperation("获取销售对应的客户列表")
    @PostMapping("/customerPageByMarket")
    public ResultBean customerPageByMarket(@RequestBody @Valid CustomerPageDto dto) {
        return ResultBean.success(customerService.customerPageByMarket(dto));
    }

    @GetMapping("/findByRoleCode")
    @ApiOperation("根据角色的用户信息")
    public ResultBean findByRoleCode(@RequestParam(value = "roleCodes", required=false) List<String> roleCodes){
        Map<String, UserAndRoleCodeVo> users = userService.findByRoleCode(roleCodes);
        return  ResultBean.success(users);
    }

}
